﻿using Apps.BLL.Core;
using Apps.Common;
using Apps.IBLL.LMS;
using Apps.IDAL.LMS;
using Apps.Locale;
using Apps.Models;
using LinqToExcel;
using Microsoft.Practices.Unity;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using System.Transactions;

namespace Apps.BLL.LMS
{
    public partial class LMS_TEMPLATE_RELATIONBLL : Virtual_LMS_TEMPLATE_RELATIONBLL, ILMS_TEMPLATE_RELATIONBLL
    {

        public List<V_LMS_TEMPLATE_RELATION> GetListById(ref GridPager pager, string id)
        {
            try
            {
                using (DBContainer db = new DBContainer())
                {
                    List<V_LMS_TEMPLATE_RELATION> queryData = db.V_LMS_TEMPLATE_RELATION.Where(a => true).ToList();
                    if (string.IsNullOrEmpty(id))
                    {
                        queryData= queryData.Where(a => a.MINE_MATERIAL_ID == id).ToList();
                    }
                    return queryData;
                }
            }
            catch (Exception ex)
            {

                throw;
            }
        }
    }
    public class Virtual_LMS_TEMPLATE_RELATIONBLL
    {

        [Dependency]
        public ILMS_TEMPLATE_RELATIONRepository m_Rep { get; set; }
        public virtual List<LMS_TEMPLATE_RELATION> GetList(ref GridPager pager, string queryStr)
        {

            IQueryable<LMS_TEMPLATE_RELATION> queryData = null;
            if (!string.IsNullOrWhiteSpace(queryStr))
            {
                queryData = m_Rep.GetList(
                                a => a.TEMPLATE_ID.Contains(queryStr)
                                );
            }
            else
            {
                queryData = m_Rep.GetList();
            }
            pager.totalRows = queryData.Count();
            //排序
            queryData = LinqHelper.SortingAndPaging(queryData, pager.sort, pager.order, pager.page, pager.rows);
            return CreateModelList(ref queryData);
        }

        public virtual List<LMS_TEMPLATE_RELATION> GetPagerList(ref GridPager pager, Expression<Func<LMS_TEMPLATE_RELATION, bool>> whereLambda)
        {
            IQueryable<LMS_TEMPLATE_RELATION> queryData = m_Rep.GetList(whereLambda);

            pager.totalRows = queryData.Count();
            //排序
            queryData = LinqHelper.SortingAndPaging(queryData, pager.sort, pager.order, pager.page, pager.rows);
            return CreateModelList(ref queryData);
        }

        public virtual List<LMS_TEMPLATE_RELATION> GetList(Expression<Func<LMS_TEMPLATE_RELATION, bool>> whereLambda)
        {
            IQueryable<LMS_TEMPLATE_RELATION> queryData = m_Rep.GetList(whereLambda);
            return CreateModelList(ref queryData);
        }

        public virtual List<LMS_TEMPLATE_RELATION> GetListByParentId(ref GridPager pager, string queryStr, object parentId)
        {
            return new List<LMS_TEMPLATE_RELATION>();
        }

        public virtual List<LMS_TEMPLATE_RELATION> CreateModelList(ref IQueryable<LMS_TEMPLATE_RELATION> queryData)
        {
            List<LMS_TEMPLATE_RELATION> modelList = queryData.ToList();
            return modelList;
        }

        public virtual bool Create(ref ValidationErrors errors, LMS_TEMPLATE_RELATION model)
        {
            try
            {
                LMS_TEMPLATE_RELATION entity = m_Rep.GetById(model.MINE_MATERIAL_ID);
                if (entity != null)
                {
                    errors.Add(Resource.PrimaryRepeat);
                    return false;
                }
                entity = new LMS_TEMPLATE_RELATION();
                entity.MINE_MATERIAL_ID = model.MINE_MATERIAL_ID;
                entity.TEMPLATE_ID = model.TEMPLATE_ID;
                entity.UPDATE_USER = model.UPDATE_USER;
                entity.UPDATE_TIME = model.UPDATE_TIME;

                if (m_Rep.Create(entity))
                {
                    return true;
                }
                else
                {
                    errors.Add(Resource.InsertFail);
                    return false;
                }
            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }

        public virtual bool Deletes(ref ValidationErrors errors, Expression<Func<LMS_TEMPLATE_RELATION, bool>> whereLambda)
        {
            try
            {
                using (DBContainer db = new DBContainer())
                {
                    foreach (var entity in db.LMS_TEMPLATE_RELATION.Where(whereLambda))
                    {
                        db.Entry(entity).State = System.Data.Entity.EntityState.Deleted;
                    }
                    if (db.SaveChanges() > 0)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }

        public virtual bool Delete(ref ValidationErrors errors, object id)
        {
            try
            {
                if (m_Rep.Delete(id) == 1)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }

        public virtual bool Delete(ref ValidationErrors errors, object[] deleteCollection)
        {
            try
            {
                if (deleteCollection != null)
                {
                    using (TransactionScope transactionScope = new TransactionScope())
                    {
                        if (m_Rep.Delete(deleteCollection) == deleteCollection.Length)
                        {
                            transactionScope.Complete();
                            return true;
                        }
                        else
                        {
                            Transaction.Current.Rollback();
                            return false;
                        }
                    }
                }
                return false;
            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }

        public virtual bool Edit(ref ValidationErrors errors, LMS_TEMPLATE_RELATION model)
        {
            try
            {
                LMS_TEMPLATE_RELATION entity = m_Rep.GetById(model.MINE_MATERIAL_ID);
                if (entity == null)
                {
                    errors.Add(Resource.Disable);
                    return false;
                }
                entity.MINE_MATERIAL_ID = model.MINE_MATERIAL_ID;
                entity.TEMPLATE_ID = model.TEMPLATE_ID;
                entity.UPDATE_USER = model.UPDATE_USER;
                entity.UPDATE_TIME = model.UPDATE_TIME;
                if (m_Rep.Edit(entity))
                {
                    return true;
                }
                else
                {
                    errors.Add(Resource.NoDataChange);
                    return false;
                }
            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }

        public virtual LMS_TEMPLATE_RELATION GetById(object id)
        {
            if (IsExists(id))
            {
                LMS_TEMPLATE_RELATION entity = m_Rep.GetById(id);
                LMS_TEMPLATE_RELATION model = new LMS_TEMPLATE_RELATION();
                model.MINE_MATERIAL_ID = entity.MINE_MATERIAL_ID;
                model.TEMPLATE_ID = entity.TEMPLATE_ID;
                model.UPDATE_USER = entity.UPDATE_USER;
                model.UPDATE_TIME = entity.UPDATE_TIME;
                return model;
            }
            else
            {
                return null;
            }
        }

        public virtual LMS_TEMPLATE_RELATION GetItem(Expression<Func<LMS_TEMPLATE_RELATION, bool>> whereLambda)
        {

            LMS_TEMPLATE_RELATION entity = m_Rep.GetItem(whereLambda);
            if (entity == null) return null;

            LMS_TEMPLATE_RELATION model = new LMS_TEMPLATE_RELATION();
            model.MINE_MATERIAL_ID = entity.MINE_MATERIAL_ID;
            model.TEMPLATE_ID = entity.TEMPLATE_ID;
            model.UPDATE_USER = entity.UPDATE_USER;
            model.UPDATE_TIME = entity.UPDATE_TIME;
            return model;
        }

        public virtual bool CheckImportData(string fileName, List<LMS_TEMPLATE_RELATION> list, ref ValidationErrors errors)
        {

            var targetFile = new FileInfo(fileName);

            if (!targetFile.Exists)
            {

                errors.Add("导入的数据文件不存在");
                return false;
            }

            var excelFile = new ExcelQueryFactory(fileName);

            //对应列头
            excelFile.AddMapping<LMS_TEMPLATE_RELATION>(x => x.MINE_MATERIAL_ID, "MINE_MATERIAL_ID");
            excelFile.AddMapping<LMS_TEMPLATE_RELATION>(x => x.TEMPLATE_ID, "TEMPLATE_ID");
            excelFile.AddMapping<LMS_TEMPLATE_RELATION>(x => x.UPDATE_USER, "UPDATE_USER");
            excelFile.AddMapping<LMS_TEMPLATE_RELATION>(x => x.UPDATE_TIME, "UPDATE_TIME");
            //SheetName
            var excelContent = excelFile.Worksheet<LMS_TEMPLATE_RELATION>(0);
            int rowIndex = 1;
            //检查数据正确性
            foreach (var row in excelContent)
            {
                var errorMessage = new StringBuilder();
                var entity = new LMS_TEMPLATE_RELATION();
                entity.MINE_MATERIAL_ID = row.MINE_MATERIAL_ID;
                entity.TEMPLATE_ID = row.TEMPLATE_ID;
                entity.UPDATE_USER = row.UPDATE_USER;
                entity.UPDATE_TIME = row.UPDATE_TIME;

                //=============================================================================
                if (errorMessage.Length > 0)
                {
                    errors.Add(string.Format(@"                第 {0} 列发现错误：{1}{2}", rowIndex,
                        errorMessage,
                        "<br/>"));
                }
                list.Add(entity);
                rowIndex += 1;
            }
            if (errors.Count > 0)
            {
                return false;
            }
            return true;
        }

        public virtual void SaveImportData(IEnumerable<LMS_TEMPLATE_RELATION> list)
        {
            try
            {
                using (DBContainer db = new DBContainer())
                {
                    foreach (var model in list)
                    {
                        LMS_TEMPLATE_RELATION entity = new LMS_TEMPLATE_RELATION();
                        entity.MINE_MATERIAL_ID = model.MINE_MATERIAL_ID;
                        entity.TEMPLATE_ID = model.TEMPLATE_ID;
                        entity.UPDATE_USER = model.UPDATE_USER;
                        entity.UPDATE_TIME = model.UPDATE_TIME;

                        db.LMS_TEMPLATE_RELATION.Add(entity);
                    }
                    db.SaveChanges();
                }
            }
            catch
            {
                throw;
            }
        }

        public virtual bool IsExists(object id)
        {
            return m_Rep.IsExist(id);
        }

        public void Dispose()
        {

        }

    }
}
